package com.my.selectcoursesystem.controller;

import com.my.selectcoursesystem.bean.Course;
import com.my.selectcoursesystem.bean.Student;
import com.my.selectcoursesystem.bean.Teacher;
import com.my.selectcoursesystem.service.serviceImpl.CourseServiceImpl;
import com.my.selectcoursesystem.service.serviceImpl.StudentServiceImpl;
import com.my.selectcoursesystem.service.serviceImpl.TeacherServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private StudentServiceImpl studentService;
    @Autowired
    private TeacherServiceImpl teacherService;

    private Student student;
    private Teacher teacher;
    private String condition;

    public LoginController(StudentServiceImpl studentService, TeacherServiceImpl teacherService) {
        this.studentService = studentService;
        this.teacherService = teacherService;
    }

    public LoginController() {
    }

    public void addCookie(HttpServletResponse response, String name, String value) {
        Cookie cookie = new Cookie(name, value);//创建新cookie
        cookie.setMaxAge(30 * 60);// 设置存在时间为5分钟
        response.addCookie(cookie);//将cookie添加到response的cookie数组中返回给客户端
    }

    public String getCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();//根据请求数据，找到cookie数组
        String value = "";
        if (null == cookies) {//如果没有cookie数组
            System.out.println("没有cookie");
            value = "";
        } else {
            for (Cookie cookie : cookies) {
                System.out.println("cookieName:" + cookie.getName() + ",cookieValue:" + cookie.getValue());
                if (cookie.getName().equals(name)) {
                    value = cookie.getValue();
                    break;
                }
            }
        }
        return value;
    }

    public void delCookie(HttpServletRequest request, HttpServletResponse response, String name) {
        Cookie[] cookies = request.getCookies();
        if (null == cookies) {
            System.out.println("没有cookie");
        } else {
            for (Cookie cookie : cookies) {
                //如果找到同名cookie，就将value设置为null，将存活时间设置为0，再替换掉原cookie，这样就相当于删除了。
                if (cookie.getName().equals(name)) {
                    cookie.setValue("");
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                    break;
                }
            }
        }
    }

    // 登录页面
    @RequestMapping("/")
    public String Login(Model model, HttpServletRequest request) {
        String s_id = this.getCookie(request, "sid");
        String s_name = this.getCookie(request, "sname");
        if (!s_id.equals("") && !s_name.equals("")) {
            return "redirect:/index?u=" + this.condition;
        }
        return "index";
    }

    @RequestMapping("/index")
    public String Index(String u, Model model, HttpServletRequest request) {
        String s_id = this.getCookie(request, "sid");
        String s_name = this.getCookie(request, "sname");
        if ((this.student == null && this.teacher == null) || (s_id.equals("") && s_name.equals(""))) {
            return "index";
        }
        model.addAttribute("sname", s_name);
        model.addAttribute("sid", s_id);
        if ("student".equals(u)) {
            return "student/studentFace";
        } else if ("teacher".equals(u)) {
            return "teacher/teacherFace";
        } else if ("admin".equals(u)) {
            return "admin/adminFace";
        }
        return "index";
    }

    // 处理学生登录
    @RequestMapping(value = "/studentlogin", method = RequestMethod.POST)
    public String StudentLogin(String sid, String spassword, String optionsRadios1, Model model, HttpServletResponse response) {
        this.condition = optionsRadios1;
        this.student = this.studentService.getStudent(sid, spassword);
        if (this.student != null) {
            this.addCookie(response,"sname", this.student.getSname());
            this.addCookie(response,"sid", this.student.getSid());
            return "redirect:/index?u=student";
        }else {
            model.addAttribute("msg", "您输入的用户名或密码错误！");
            return "fail";
        }
    }

    // 处理老师登录
    @RequestMapping(value = "/teacherlogin", method = RequestMethod.POST)
    public String teacherLogin(String tid, String tpassword, String optionsRadios1, Model model, HttpServletResponse response) {
        this.teacher = this.teacherService.getTeacher(tid, tpassword);
        if (this.teacher != null) {
            this.addCookie(response, "sname", this.teacher.getTname());
            this.addCookie(response, "sid", this.teacher.getTid());
            return "redirect:/index?u=teacher";
        } else {
            model.addAttribute("msg", "您输入的用户名或密码错误！");
            return "fail";
        }
    }

    // 处理管理员登录
    @RequestMapping(value = "/adminlogin", method = RequestMethod.POST)
    public String adminLogin(String tid, String tpassword, String optionsRadios1, Model model, HttpServletResponse response) {
        this.teacher = this.teacherService.getTeacher(tid, tpassword);
        if (this.teacher != null) {
            this.addCookie(response, "sname", this.teacher.getTname());
            this.addCookie(response, "sid", this.teacher.getTid());
            return "redirect:/index?u=admin";
        } else {
            model.addAttribute("msg", "您输入的用户名或密码错误！");
            return "fail";
        }
    }

    // 注销
    @RequestMapping(value = "/logout")
    public String logOut(HttpServletRequest request, HttpServletResponse response) {
        this.delCookie(request, response, "sid");
        this.delCookie(request, response, "sname");
        return "redirect:/";
    }
}
